package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    @Override
    public void myRemoveById(Long ids) {
        //判断菜品分类和套餐分类中是否包含菜品如果有菜品则显示删除失败如果没有显示删除成功

        //构造条件构造器,先判断
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //注入条件信息查询两个条件相同的项目
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, ids);
        int dishCount = dishService.count(dishLambdaQueryWrapper);

        if (dishCount > 0) {
            //已经关联菜品抛出异常
            throw new CustomException("该菜品分类关联了菜品，不能删除");

        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();

        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, ids);

        int setmealCount = setmealService.count(setmealLambdaQueryWrapper);

        if (setmealCount > 0) {
            //已经关联菜品抛出异常
            throw new CustomException("该套餐分类关联了菜品，不能删除");
        }

        //如果两者都等于零则删除成功
        super.removeById(ids);

    }


}
